﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Hkwl.WorldExpo.Merchant.Common;
using Hkwl.WorldExpo.Merchant.Model;
using Hkwl.WorldExpo.Merchant.Service;
using System.Collections.Generic;
using System.IO;

namespace Hkwl.WorldExpo.Merchant.Web
{
    public partial class MMSCreat : BaseCheckPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int projectId = int.Parse(Request.QueryString["ProjectId"]);
            int mmsPage = int.Parse(Request.QueryString["MmsPage"]);
            NxProject nxProject = ServiceLocator.IProjectService.GetNxProjectById(projectId);
            IList<NxMms> nxMms = ServiceLocator.IProjectService.GetNxMmsCollection(projectId);
            this.lbMSSTitle.Text = nxProject.MmsTitle;
            if (mmsPage != 1)
                this.btnback.Visible = true;
            else
                this.btnback.Visible = false;
            this.lbMmsPage.Text = (String)GetGlobalResourceObject("Default", "THE") + mmsPage.ToString() + (String)GetGlobalResourceObject("Default", "PAGE");
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            //上传彩信图片
            try
            {
                if (fuMMSImage.HasFile)
                {
                    //判断图片格式是否正确
                    string photoExt = Path.GetExtension(this.fuMMSImage.FileName).ToLower();
                    if (photoExt == ".gif" || photoExt == ".jpg")
                    {
                        string name = this.fuMMSImage.PostedFile.FileName; //文件路径
                        //System.Drawing.Image image = System.Drawing.Image.FromFile(name);
                        //if (image.Width > 240 || image.Height > 240)
                        //{
                        //    JavaScript.WriteJs("jAlert('您上传的图片大小不能高于240像素*240像素','" + (String)GetGlobalResourceObject("Default", "NOTE") + "',null)", this.Page);
                        //    return;
                        //}
                        FileInfo fileInfo = new FileInfo(name);

                        Random rd = new Random();
                        string fileName = DateTime.Now.ToString("yyyyMMddmmhhss") + rd.Next(10, 99) + photoExt;   //文件名称
                        string sFileName = "x_" + fileName;             //缩略图文件名 

                        string filePath = Server.MapPath("~/ImgUpLoad/" + fileName);    //服务器端文件路径
                        string sFilePath = Server.MapPath("~/ImgUpLoad/" + sFileName);  //缩略图文件路径

                        int w = 240;
                        int h = 0;

                        this.fuMMSImage.SaveAs(filePath);
                        ThumbNail.MakeThumNail(filePath, sFilePath, w, h, "W");

                        //新增彩信
                        NxMms nxMms = new NxMms();
                        //nxMms.MmsPage = int.Parse(ddlMmsPage.SelectedValue.ToString());
                        nxMms.MmsPage = int.Parse(Request.QueryString["MmsPage"]);
                        nxMms.MmsPic = @"ImgUpLoad/" + sFileName;//只保存缩略图地址
                        nxMms.ProjectId = int.Parse(Request.QueryString["ProjectId"]);
                        nxMms.MmsPosition = int.Parse(rblMMSPosition.SelectedValue);
                        nxMms.MmsText = this.txtMMSText.Text.Trim();
                        if (int.Parse(this.txtDelayTime.Text.Trim()) <= 30)
                        {
                            nxMms.DelayTime = int.Parse(this.txtDelayTime.Text.Trim());
                        }
                        else
                        {
                            nxMms.DelayTime = 30;
                        }


                        if (ServiceLocator.IProjectService.InsertNxMms(nxMms))
                        {
                            Response.Redirect("MMSView.aspx?MmsId=" + nxMms.MmsId + "&ProjectId=" + Request.QueryString["ProjectId"]);
                        }

                    }
                    else
                    {
                        JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default", "PICTURE_FORMAT") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "',null)", this.Page);
                        return;
                    }
                }
                else
                {
                    if (!String.IsNullOrEmpty(this.txtMMSText.Text.Trim()))
                    {
                        //新增彩信
                        NxMms nxMms = new NxMms();
                        nxMms.MmsPage = int.Parse(Request.QueryString["MmsPage"]);
                        nxMms.ProjectId = int.Parse(Request.QueryString["ProjectId"]);
                        nxMms.MmsPosition = int.Parse(rblMMSPosition.SelectedValue);
                        nxMms.MmsText = this.txtMMSText.Text.Trim();
                        if (int.Parse(this.txtDelayTime.Text.Trim()) <= 30)
                        {
                            nxMms.DelayTime = int.Parse(this.txtDelayTime.Text.Trim());
                        }
                        else
                        {
                            nxMms.DelayTime = 30;
                        }


                        if (ServiceLocator.IProjectService.InsertNxMms(nxMms))
                        {
                            Response.Redirect("MMSView.aspx?MmsId=" + nxMms.MmsId + "&ProjectId=" + Request.QueryString["ProjectId"]);
                        }
                    }
                    else
                    {
                        JavaScript.WriteJs("jAlert('" + (String)GetGlobalResourceObject("Default", "MMSTEXT_EMPTY") + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "',null)", this.Page);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                JavaScript.WriteJs("jAlert('" + ex.ToString() + "','" + (String)GetGlobalResourceObject("Default", "NOTE") + "',null)", this.Page);
                return;
            }
        }

        protected void btnback_Click(object sender, EventArgs e)
        {
            int projectId = int.Parse(Request.QueryString["ProjectId"]);
            int mmsPage = int.Parse(Request.QueryString["MmsPage"]);
            NxMms nxMms = ServiceLocator.IProjectService.GetNxMmsByPage(projectId,mmsPage-1);
            if (nxMms != null)
            {
                int mmsId = nxMms.MmsId;
                Response.Redirect(string.Format("MMSView.aspx?MmsId={0}&ProjectId={1}", mmsId, projectId));
            }
        }
    }
}
